﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using JiZiYan.Models;
using System.Web.Mvc;

namespace JiZiYan.Controllers
{
    public class adminListController : Controller
    {
        // GET: adminList
        public ActionResult Index()
        {
            using (JiZiYanEntities2 db = new JiZiYanEntities2())
            {
                var arr = db.Roles.Where(x => true).ToList();
                ViewBag.RolesName = arr;
            }
            return View();
        }
        /// <summary>
        /// 分页方法
        /// </summary>
        /// <returns></returns>
        public ActionResult Search()
        {
            int page, limit;
            if (!int.TryParse(Request.Params["page"], out page))
            {
                page = 1;
            }
            if (!int.TryParse(Request.Params["limit"], out limit))
            {
                limit = 10;
            }
            string name = Request.Params["name"];
            JiZiYanEntities2 db = new JiZiYanEntities2();
            var arr = db.View_AllUser.Where(x => true);
            if (!string.IsNullOrWhiteSpace(name))
            {
                arr = arr.Where(x => x.LoginName.Contains(name));
            }
            var count = arr.Count();
            var data = arr.OrderByDescending(x => x.CreateTime).Skip((page - 1) * limit).Take(limit).ToList();
            return Json(new { code = 0, count = count, data = data }, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 禁用方法
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Forbidden(Guid id)
        {
            using (JiZiYanEntities2 db=new JiZiYanEntities2())
            {
                var arr = db.Users.SingleOrDefault(x => x.Id == id);
                ViewBag.Islockout = arr;
                if (arr.IsLockout == true)
                {
                    arr.IsLockout = false;
                    var count = db.SaveChanges();
                    if (count > 0)
                    {                       
                        return Json(new { success = true, msg = "解除冻结成功" });
                    }
                    else
                    {
                        return Json(new { success = false, msg = "解除冻结失败" });
                    }
                }
                else
                {
                    arr.IsLockout = true;
                    var count = db.SaveChanges();
                    if (count > 0)
                    {
                        return Json(new { success = true, msg = "冻结成功" });
                    }
                    else
                    {
                        return Json(new { success = false, msg = "冻结失败" });
                    }
                }
                
            }
        }
        /// <summary>
        /// 修改用户权限页面
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult edit(Guid id)
        {
            JiZiYanEntities2 db = new JiZiYanEntities2();
            {
                var arr = db.Roles.Where(x => true);
                ViewBag.RolesName = arr;
                //查到所有权限
                var editName = db.Users.SingleOrDefault(x => x.Id == id);
                ViewBag.editName = editName;
                //查到当前用户权限                
                var Allrol = db.Roles.ToList();
                var norol = db.UserRoles.Where(x => x.UserId == id).ToList();
                //当前拥有权限
                List<Roles> qz = new List<Roles>();
                for (int i = 0; i < norol.Count(); i++)
                {
                    var dqqx = norol[i].RoleId;
                    var dqid = db.Roles.SingleOrDefault(x => x.Id == dqqx);
                    qz.Add(dqid);
                    Allrol.Remove(dqid);                   
                }
                ViewBag.userrolesyes = qz;   //当前拥有权限
                ViewBag.userrolesno = Allrol;     //当前未拥有权限                 
            }
            return View();
        }
        /// <summary>
        /// 修改用户权限方法
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult edit(Guid Id,List<Guid>AllRoles)
        {
            JiZiYanEntities2 db=new JiZiYanEntities2();
            {             
                var romallroles = db.UserRoles.Where(x => x.UserId == Id).ToList();
                //获取当前用户的所有权限
                for (int i = 0; i < romallroles.Count(); i++)
                {
                    db.UserRoles.Remove(romallroles[i]);
                    //删除当前用户所有权限
                }
                for (int i = 0; i < AllRoles.Count(); i++)
                {
                    UserRoles r = new UserRoles();
                    r.RoleId = AllRoles[i];
                    r.UserId =Id;
                    r.Id = Guid.NewGuid(); ;
                    db.UserRoles.Add(r);
                }
                var hg=db.SaveChanges();
                if (hg>=1)
                {
                    return Json(new { success = true, msg = "修改成功" });
                }
                else
                {
                    return Json(new { success = false, msg = "修改失败" });
                }                
            }
        }
        /// <summary>
        /// 重置密码
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult reset(Guid id)
        {
            JiZiYanEntities2 db = new JiZiYanEntities2();
            var arr = db.Users.SingleOrDefault(x => x.Id == id);
            arr.Password = "123456";
            var count = db.SaveChanges();
            if (count > 0)
            {
                return Json(new { success = true, msg = "重置密码成功" });
            }
            else
            {
                return Json(new { success = false, msg = "密码和最初密码一致，无需重置" });
            }
        }

    }
}